package com.qnzw.zjmy.ad.kaku.reward;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.anythink.rewardvideo.api.ATRewardVideoAd;
import com.anythink.rewardvideo.api.ATRewardVideoListener;
import com.qnzw.zjmy.ad.impl.AdRevenueListenerImpl;
import com.qnzw.zjmy.ad.inter.RewardAdCallbackInter;
import com.qnzw.zjmy.ad.inter.RewardAdInter;
import com.qnzw.zjmy.home.ui.enums.TaKuAdPositionEnum;

import cn.hutool.core.util.ObjectUtil;

/**
 * 激励视频
 */
public class TaKuRewardAdWithdrawal implements ATRewardVideoListener {
    private static String TAG = "TaKuRewardAdWithdrawal";

    private ATRewardVideoAd mRewardVideoAd;
    private RewardAdInter rewardAdInter;
    private Context context;
    private String toastContent;
    private AlertDialog alertDialog;

    private boolean isLoad = false;

    private RewardAdCallbackInter rewardAdCallbackInter;

    private static volatile TaKuRewardAdWithdrawal instance;

    private boolean isRewardValid = false;

    private String token;

    private boolean loadedFail = false;

    private Activity activity;

    private String placementId;

    private TaKuRewardAdWithdrawal() {
    }

    public static synchronized TaKuRewardAdWithdrawal getInstance() {
        if (ObjectUtil.isNull(instance)) {
            synchronized (TaKuRewardAdWithdrawal.class) {
                if (ObjectUtil.isNull(instance)) {
                    instance = new TaKuRewardAdWithdrawal();
                }
            }
        }
        return instance;
    }

    public void loadAd(Context context, String placementId) {
        this.placementId = placementId;
        mRewardVideoAd = new ATRewardVideoAd(context, placementId);
        mRewardVideoAd.setAdListener(this);
        mRewardVideoAd.load();
    }

    public void showAd(Activity activity) {
        this.activity = activity;
        if (ObjectUtil.isNull(mRewardVideoAd)) {
            loadedFail = true;
            loadAd(activity.getApplicationContext(), placementId);
            return;
        }
        mRewardVideoAd.setAdRevenueListener(new AdRevenueListenerImpl(activity,token, TaKuAdPositionEnum.AD_REWARD_POSITION.getKey(), "2"));
        if (mRewardVideoAd.isAdReady()) {
            if (alertDialog.isShowing()) {
                alertDialog.dismiss();
            }
            mRewardVideoAd.setAdListener(this);
            mRewardVideoAd.show(activity);
        } else {
            //重新加载
            mRewardVideoAd.setAdListener(this);
            loadedFail = true;
            mRewardVideoAd.load();
        }
    }

    @Override
    public void onRewardedVideoAdLoaded() {
        if (ObjectUtil.isNotNull(activity) && loadedFail) {
            if (alertDialog.isShowing()) {
                alertDialog.dismiss();
            }
            mRewardVideoAd.show(activity);
            loadedFail = false;
        }
    }

    @Override
    public void onRewardedVideoAdFailed(AdError adError) {
        Log.e(TAG, "onRewardedVideoAdFailed:" + adError.getFullErrorInfo());
        if (isLoad) {
            isLoad = false;
            if (alertDialog.isShowing()) {
                alertDialog.dismiss();
            }
            Toast.makeText(context, "视频加载失败", Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onRewardedVideoAdPlayStart(ATAdInfo atAdInfo) {
        Log.e(TAG, "onRewardedVideoAdFailed:" + atAdInfo.getAdNetworkType());
        mRewardVideoAd.load();
    }

    @Override
    public void onRewardedVideoAdPlayEnd(ATAdInfo atAdInfo) {
    }

    @Override
    public void onRewardedVideoAdPlayFailed(AdError adError, ATAdInfo atAdInfo) {
        Log.e(TAG, "onRewardedVideoAdPlayFailed:" + adError.getFullErrorInfo());
    }

    @Override
    public void onRewardedVideoAdClosed(ATAdInfo atAdInfo) {
        //广告关闭
        if (getInstance().isRewardValid) {
            Double ecpm = atAdInfo.getPublisherRevenue(); //展示广告的价格 0.105234661721
            ecpm = ecpm * 100000;
            rewardAdInter.rewardAdSign(String.valueOf(ecpm));
            getInstance().isRewardValid = false;
        } else {
            Toast.makeText(context, toastContent, Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onRewardedVideoAdPlayClicked(ATAdInfo atAdInfo) {
        Log.e(TAG, "onRewardedVideoAdPlayClicked:" + atAdInfo.getAdNetworkType());
    }

    @Override
    public void onReward(ATAdInfo atAdInfo) {
        isRewardValid = true;
        Log.e(TAG, "onReward:" + atAdInfo.getAdNetworkType());
    }

    public void setRewardAdInter(RewardAdInter rewardAdInter) {
        this.rewardAdInter = rewardAdInter;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public void setToastContent(String toastContent) {
        this.toastContent = toastContent;
    }

    public void setAlertDialog(AlertDialog alertDialog) {
        this.alertDialog = alertDialog;
    }

    public void setToken(String token) {
        this.token = token;
    }
}
